// index.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo:null,
    tabClass: ["", "", "", "", ""],
    UserRankInfo:{},
    PayCreditName:"",
    orderStatistics:{},
    supportCompany: "",
    supportPhone: "",
    PayCredits:0,
    Distributor:true
  },
  //判断是否开通分销功能
  checkDistributor: function () {
    var that = this
    wx.request({
      url: app.globalData.host + '/api/Store/CheckDistributor?storeId=' + app.globalData.storeId,
      success: function (res) {
       if(res.data.Code == 0){
         that.setData({
           Distributor: res.data.Data
         })
       }
      },
    })
  },
  toShareCode:function(){
    wx.navigateTo({
      url: '/fenxiao/shareCodes/index?userid=' + app.globalData.userInfo.Uid,
    })
  },
  //编辑信息
  editorUser:function(){
    wx.navigateTo({
      url: '/pages/editor/index',
    })
  },
  /**
   * 获取店铺信息
   */
  GetVideoInfo: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Store/GetInfo?id=' + app.globalData.storeId,
      success: function (res) {
        if (res.data.Code == 0) {
          that.setData({
            videoSrc: res.data.Data.VideoSrc,
            logoSrc: 'http://static.711688.net.cn/upload/store/' + app.globalData.storeId + '/banner/' + res.data.Data.Banner,
            supportCompany: res.data.Data.SupportCompany,
            supportPhone: res.data.Data.SupportPhone
          })
        }
      }
    })
  },
  //拨打服务商电话
  callCompany: function () {
    var that = this;
    if (that.data.supportPhone == '') {
      return
    } else {
      wx.makePhoneCall({
        phoneNumber: that.data.supportPhone
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    that.setData({
      userinfo: app.globalData.userInfo
    })
    that.getOrderStatistics();
    that.GetUserInfo();
    that.GetVideoInfo();
    that.checkDistributor();
    //获取积分名称
    wx.request({
      url: app.globalData.host + '/api/Store/GetStoreCreditConfigInfo?storeId=' + app.globalData.storeId,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data.PayCreditName !=''){
            that.setData({
              PayCreditName: res.data.Data.PayCreditName
            })
          }
        }
        else {
          wx.showModal({
            title: '提示',
            content: res.data.errMsg,
            showCancel: false
          })
          return;
        }
      }
    })
  },

  //获取用户信息
  GetUserInfo:function(){
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/User/GetUserInfo?uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        if (res.data.Code == 0) {
          var UserRankInfo = res.data.Data.UserRankInfo
          UserRankInfo.Avatar = 'http://static.711688.net.cn' + '/upload/userrank/source/' + UserRankInfo.Avatar
          that.setData({
            UserRankInfo: UserRankInfo,
            PayCredits: res.data.Data.UserInfo.PayCredits
          })
        }
        else {
          wx.showModal({
            title: '提示',
            content: res.data.errMsg,
            showCancel: false
          })
          return;
        }
      }
    })
  },
  getOrderStatistics: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/order/statistics',
      data: { uid: app.globalData.userInfo.Uid },
      success: (res) => {
        wx.hideLoading();
        if (res.data.Code == 0) {
          var tabClass = that.data.tabClass;
          if (res.data.Data.count_id_no_pay > 0) {
            tabClass[1] = "circle"
          }
          if (res.data.Data.count_id_no_transfer > 0) {
            tabClass[2] = "circle"
          }
          if (res.data.Data.count_id_no_confirm > 0) {
            tabClass[3] = "circle"
          }
          if (res.data.Data.count_id_success > 0) {
            tabClass[4] = "circle"
          }

          that.setData({
            tabClass: tabClass,
            orderStatistics:res.data.Data
          });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrderStatistics()
  },


  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作
    wx.stopPullDownRefresh();
  },
})